﻿using System.Reflection;
using System.Threading;
using System.Windows.Forms;
using Helpers;
using Helpers.Language;
using Helpers.Properties;
using Settings = Helpers.Properties.Settings;

namespace Launcher
{
    public partial class Launcher : Form
    {
        private Settings AppSettings = Helpers.Properties.Settings.Default;
        private Updater UpdateTool { get; set; }

        public delegate void PostPatchingDelegate();

        public Launcher(string[] args)
        {
            LangRes.SetLanguage(AppSettings.DefaultLanguage);
            InitializeComponent();
            UpdateTool = new Updater(launcherProgressBar, launcherProgressBarLabel, EnableGames);
            versionCopyrightLabel.Text = LangRes.Get("Version") + " " + Assembly.GetExecutingAssembly().GetName().Version.ToString();
            CheckArguments(args);
        }

        public void EnableGames()
        {
            buttonWOT.Enabled = true;
        }

        private void CheckArguments(string[] args)
        {
            if (args.Length == 0)
            {
                UpdateTool.RunUpdater();
            }
            foreach (var arg in args)
            {
                if (arg == AppSettings.CleanupArgument)
                {
                    Thread.Sleep(500);
                    UpdateTool.CleanupTemp();
                    UpdateTool.RunUpdater();
                }
                else if (arg == AppSettings.SelfUpdateArgument)
                {
                    Thread.Sleep(500);
                    UpdateTool.RunSelfUpdater();
                }
            }
        }

        private void ButtonWOTClick(object sender, System.EventArgs e)
        {
//            var view = new WOTMainWindowView();
//            var controller = new MainWindowController(ShowLauncher, view, Game.WOT);
//            controller.LoadView();
//            view.Show();
//            this.Hide();
        }

        private void ButtonWOWPClick(object sender, System.EventArgs e)
        {
            MessageBox.Show(LangRes.Get("GameNotSupported"), LangRes.Get("NotSupported"), MessageBoxButtons.OK,
                            MessageBoxIcon.Warning);
        }

        private void ButtonWOBSClick(object sender, System.EventArgs e)
        {
            MessageBox.Show(LangRes.Get("GameNotSupported"), LangRes.Get("NotSupported"), MessageBoxButtons.OK,
                                        MessageBoxIcon.Warning);
        }

        public void ShowLauncher(bool exitProgram)
        {
            if (exitProgram) Close();
            else this.Show();
        }
    }
}
